{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4634a7ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "import torch.nn as nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2be99377",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3]).reshape(-1,1)  # 1行3列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fcc678d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3]).reshape(-1,1)[0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3ffe40cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(3,1)[0].detach().numpy().shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "13bfbc83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.7567, -1.6236, -1.5365, -0.0953,  0.4155, -0.8827,  1.1012,  1.5133,\n",
       "         -0.6855,  0.5350]], grad_fn=<EmbeddingBackward0>)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embed = nn.Embedding(100,10)\n",
    "d = torch.Tensor([1]).long()\n",
    "\n",
    "# d = torch.randint(10, 20, (3,4))\n",
    "\n",
    "embed(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "65c693e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0.7567, -1.6236, -1.5365, -0.0953,  0.4155, -0.8827,  1.1012,\n",
       "           1.5133, -0.6855,  0.5350]]], grad_fn=<ViewBackward0>)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embed(d).view(1,1,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c8263ba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "3d4c0b01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([5, 3, 20])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 3, 20])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "gru = nn.GRU(10,20,num_layers=3)\n",
    "\n",
    "input = torch.randn(5,3,10)\n",
    "h0 = torch.randn(3,3,20)\n",
    "res = gru(input,h0)\n",
    "display(res[0].shape,res[1].shape)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "cf0c7553",
   "metadata": {},
   "outputs": [],
   "source": [
    "linear1 = nn.Linear(5,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "0e97ddad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.9710,  0.3616,  0.6815, -1.1318, -0.2898, -0.5768, -1.2290,  0.6013,\n",
       "         -1.4233,  0.4604],\n",
       "        [-0.5514, -0.5807,  0.0843, -0.1812, -0.5173,  0.4281,  0.4245,  0.2283,\n",
       "          0.2159,  0.0854],\n",
       "        [-0.8196,  0.5826, -0.4577,  1.0030,  0.1413, -1.1778, -0.2933,  1.0048,\n",
       "          0.8986,  0.0091]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input1 = torch.randn(3,5)\n",
    "r = linear1(input1)\n",
    "r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "118e089d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.4825, -2.0919, -1.7720, -3.5852, -2.7433, -3.0303, -3.6824, -1.8521,\n",
       "         -3.8767, -1.9930],\n",
       "        [-2.8839, -2.9132, -2.2482, -2.5137, -2.8498, -1.9044, -1.9080, -2.1042,\n",
       "         -2.1166, -2.2471],\n",
       "        [-3.4581, -2.0560, -3.0962, -1.6355, -2.4973, -3.8164, -2.9318, -1.6337,\n",
       "         -1.7399, -2.6294]], grad_fn=<LogSoftmaxBackward0>)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nn.LogSoftmax(dim=1)(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "8fbfe454",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Input Tensor:\n",
      "tensor([[ 1.2780, -0.3107, -0.5648, -0.0599, -1.0103],\n",
      "        [-1.7828, -0.2069, -0.0182, -1.2375, -1.7445],\n",
      "        [-0.0817, -0.4505, -0.1923, -0.4472,  0.3045]])\n",
      "\n",
      "Log-Softmax Output:\n",
      "tensor([[-0.5460, -2.1348, -2.3888, -1.8839, -2.8343],\n",
      "        [-2.6698, -1.0940, -0.9053, -2.1246, -2.6316],\n",
      "        [-1.5586, -1.9274, -1.6692, -1.9241, -1.1724]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "\n",
    "# 创建一个随机张量作为输入\n",
    "input_tensor = torch.randn(3, 5)\n",
    "print(\"Input Tensor:\")\n",
    "print(input_tensor)\n",
    "\n",
    "# 计算 log-softmax\n",
    "log_softmax_output = F.log_softmax(input_tensor, dim=1)\n",
    "print(\"\\nLog-Softmax Output:\")\n",
    "print(log_softmax_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2e94b46e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e6e2f767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=[[1,2,3],[2,3,4],[5,6,7]]\n",
    "random.choice(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "22dc75cb",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'module' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrandom\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'module' object is not callable"
     ]
    }
   ],
   "source": [
    "torch.random(1,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ad005230",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "a,b=torch.randn(1,10).topk(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aff53ac5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2.1729]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e3d60aae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[3]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a9899f42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(2.1729)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.squeeze().detach()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "258e97b8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
